//
//  JXStatusListViewModel.swift
//  swift高仿微博
//
//  Created by peijiaxing on 16/3/2.
//  Copyright © 2016年 peijiaxing. All rights reserved.
//

import UIKit
import AFNetworking

class JXStatusListViewModel: NSObject {

    // 定义一个数组 来接收数据
   lazy var array: [JXStatusViewModel] = [JXStatusViewModel]()
    
    // 网络请求
    func loadData(since_id: Int, max_id: Int,finish: (count :Int) ->()){
        
        let afn = AFHTTPSessionManager()
        
//
        let param = ["access_token":JXUserViewModel.shareInstance.access_token ?? "" ,
            "max_id":max_id,
            "since_id":since_id
        ]
        
        afn.GET("https://api.weibo.com/2/statuses/home_timeline.json", parameters: param,progress: nil, success: { (_ , JSON) -> Void in
            
//            print(JSON)
            
            // 获取数据 字典转模型
            if let json = JSON as? [String: NSObject]{
                if let statues = json["statuses"] as? [[String: NSObject]]{
                    
                    var temp = [JXStatusViewModel]()
                    
                    for statue in statues{
                        
                        let statusModel = JXStatusModel(dic: statue)
                                                
                        let viewModel = JXStatusViewModel()
                        
                        viewModel.statusModel = statusModel
                        
                       temp.append(viewModel)
                        
                     }
                    
                    // 对数组进行拼接
                    
                    if max_id > 0 {
                        self.array = self.array + temp
                    }else{
                        self.array = temp + self.array
                    }
                
                finish(count: temp.count)
                
                }
                
            }
            
            }) { (_ , error) -> Void in
                print(error)
        }
    }
    
}
